package com.HospitalOutpatient.service.impl;

import com.HospitalOutpatient.dao.DoctorDao;
import com.HospitalOutpatient.dao.OutpatientDao;
import com.HospitalOutpatient.dao.UserDao;
import com.HospitalOutpatient.entity.Doctor;
import com.HospitalOutpatient.entity.Outpatient;
import com.HospitalOutpatient.entity.User;
import com.HospitalOutpatient.service.DoctorService;
import com.HospitalOutpatient.vo.DoctorAndOutpatientAndUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.stream.IIOByteBuffer;
import java.util.ArrayList;
import java.util.List;

@Service("doctorService")
public class DoctorServiceImpl implements DoctorService {

    @Autowired
    private DoctorDao doctorDao;
    @Autowired
    private OutpatientDao outpatientDao;
    @Autowired
    private UserDao userDao;

    @Override
    public List<DoctorAndOutpatientAndUser> getAllDoctorByName(String doctorName) {
        if(doctorName != null && doctorName  !=""){
            doctorName = "%"+doctorName+"%";
        }
        List<Doctor> doctorList = doctorDao.selectAllDoctorByName(doctorName);
        List<DoctorAndOutpatientAndUser> doctorAndOutpatientAndUserList = new ArrayList<>();
        for (Doctor doctor : doctorList) {

            Outpatient outpatient = outpatientDao.getOutpatientById(doctor.getOutpatientId());

            User user = userDao.selectUserById(doctor.getUserId());

            DoctorAndOutpatientAndUser doctorAndOutpatientAndUser = new DoctorAndOutpatientAndUser();

            doctorAndOutpatientAndUser.setDoctorName(doctor.getDoctorName());
            doctorAndOutpatientAndUser.setPosition(doctor.getPosition());
            doctorAndOutpatientAndUser.setOutpatientName(outpatient.getOutpatientName());
            doctorAndOutpatientAndUser.setIntroduction(doctor.getIntroduction());
            doctorAndOutpatientAndUser.setDoctorId(doctor.getDoctorId());
            doctorAndOutpatientAndUser.setUserId(doctor.getUserId());

            if(user != null){
                doctorAndOutpatientAndUser.setUserName(user.getUserName());
                doctorAndOutpatientAndUser.setPassWord(user.getPassWord());
            }


            doctorAndOutpatientAndUser.setCharge(doctor.getCharge());

            doctorAndOutpatientAndUserList.add(doctorAndOutpatientAndUser);
        }
        return doctorAndOutpatientAndUserList;
    }

    @Override
    public boolean editDoctor(DoctorAndOutpatientAndUser doctorAndOutpatientAndUser) {

        Outpatient outpatient = outpatientDao.getOutpatientByName(doctorAndOutpatientAndUser.getOutpatientName());

        User user = new User();
        user.setUserName(doctorAndOutpatientAndUser.getUserName());
        user.setPassWord(doctorAndOutpatientAndUser.getPassWord());
        user.setUserId(doctorAndOutpatientAndUser.getUserId());

        System.out.println(user);

        Doctor doctor = new Doctor();
        doctor.setDoctorId(doctorAndOutpatientAndUser.getDoctorId());
        doctor.setOutpatientId(outpatient.getOutpatientId());
        doctor.setDoctorName(doctorAndOutpatientAndUser.getDoctorName());
        doctor.setPosition(doctorAndOutpatientAndUser.getPosition());
        doctor.setIntroduction(doctorAndOutpatientAndUser.getIntroduction());
        doctor.setCharge(doctorAndOutpatientAndUser.getCharge());

        System.out.println(doctor);

        int i = doctorDao.updateDoctor(doctor);
        if(i != 1){
            return false;
        }
        int i1 = userDao.updateUser(user);


        return i + i1 > 1;
    }

    @Override
    public boolean deleteDoctor(DoctorAndOutpatientAndUser doctorAndOutpatientAndUser) {
        int i = doctorDao.deleteDoctor(doctorAndOutpatientAndUser.getDoctorId());
        int i1 = userDao.deleteUser(doctorAndOutpatientAndUser.getUserId());
        return i + i1 > 1;
    }

    @Override
    public boolean addDoctor(DoctorAndOutpatientAndUser doctorAndOutpatientAndUser) {

        if(userDao.selectUserByName(doctorAndOutpatientAndUser.getUserName()) !=null){
            return false;
        }
        User user = new User();
        user.setIdentity("2");
        user.setUserName(doctorAndOutpatientAndUser.getUserName());
        user.setPassWord(doctorAndOutpatientAndUser.getPassWord());


        Outpatient outpatient = outpatientDao.getOutpatientByName(doctorAndOutpatientAndUser.getOutpatientName());

        System.out.println(outpatient.getOutpatientId());

        int i = userDao.insertUser(user);
        if(i != 1){
            return false;
        }

        Doctor doctor = new Doctor();

        doctor.setUserId(user.getUserId());
        doctor.setDoctorName(doctorAndOutpatientAndUser.getDoctorName());
        doctor.setPosition(doctorAndOutpatientAndUser.getPosition());
        doctor.setIntroduction(doctorAndOutpatientAndUser.getIntroduction());
        doctor.setCharge(doctorAndOutpatientAndUser.getCharge());
        doctor.setOutpatientId(outpatient.getOutpatientId());
        System.out.println(doctor);

        int i1 = doctorDao.insertDoctor(doctor);
        return i + i1 > 1;
    }
}
